package com.bonc.rms.controller.tms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.service.tms.IEmployeeDetailsService;
import com.bonc.rms.utils.CalendarUtil;

@Controller
@RequestMapping(value = "/employeeDetailsController")
public class EmployeeDetailsController {
	
	/**
	 * 员工工资详情
	 * 
	 * @param jobNumber
	 * @param
	 * @return
	 */
	
	@Autowired
	IEmployeeDetailsService employeeDetailsService;
	
	@RequestMapping(value = "/talentWage")
	@ResponseBody
	public String getTalentWage(@RequestParam(value = "jobNumber") String jobNumber) {
		
		List<String> wagesDatails = new ArrayList<>();
		Map<String, List<String>> wagesDatailsMap = new HashMap<>();
		wagesDatailsMap.put("wagesDatails", wagesDatails);
		
		List<Map<String, Object>> wagesMapList = employeeDetailsService.getWagesDetails(jobNumber);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(Map<String, Object> map : wagesMapList) {
			map.put("adjust_date", sdf.format(map.get("adjust_date")));
		}
		
		return JSON.toJSONString(wagesMapList);
	}

	/**
	 * 部门员工详情
	 * 
	 * @param jobNumber
	 * @param  @ResponseBody
	 * @return
	 */
	
	@RequestMapping(value = "/talentInformationDetails")
	public ModelAndView getTalentInformation(@RequestParam(value = "jobNumber") String jobNumber) {
		LogManager.Method("In  controller : EmployeeDetailsController -> getTalentInformation "
				+ "(employeeDetailsController/talentInformationDetails)");
		
		List<Map<String, Object>> informationMapList = employeeDetailsService.getEmployeeDetails(jobNumber);
		
		ModelAndView modelAndView = new ModelAndView("tmsUser/employeePage");
		modelAndView.addObject("informationMapList", informationMapList);
		
		LogManager.Method("Out controller : EmployeeDetailsController -> getTalentInformation "
				+ "(/employeeDetailsController/talentInformationDetails)");
		
		return modelAndView;
	}
	
	/**
	 * 员工调薪记录
	 * 
	 * @param jobNumber
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/talentWagesDetails")
	@ResponseBody
	public String getTalentWagesDetails(@RequestParam(value = "jobNumber") String jobNumber) {
		
		List<String> wagesDatails = new ArrayList<>();
		Map<String, List<String>> wagesDatailsMap = new HashMap<>();
		wagesDatailsMap.put("wagesDatails", wagesDatails);
		
		List<Map<String, Object>> wagesDetailsRecordList = employeeDetailsService.getEmployeeWagesDetails(jobNumber);
		
		return JSON.toJSONString(wagesDetailsRecordList);
	}
	/**
	 * 获取薪酬成本
	 * 
	 * @param jobNumber
	 * @return
	 */
	@RequestMapping(value = "/payrollCost")
	@ResponseBody
	public String getPayrollCost(
			@RequestParam(value = "jobNumber") String jobNumber,
			@RequestParam(value = "startTime") String startDate, 
    		@RequestParam(value = "endTime") String endDate ) {
		
		if( null == startDate || "".equals(startDate) || null == endDate || "".equals(endDate)) {
            String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
            startDate = year + "-01";
            endDate = year + "-12";
        }
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		dataMap.put("name", "每月工资");
		dataMap.put("type", "line");
		dataMap.put("data", employeeDetailsService.getPayrollCost(jobNumber, startDate, endDate));
		dataList.add(dataMap);
		retMap.put("title", new String[] {"每月工资"});
		retMap.put("dates", getMonthList(startDate, endDate));
		retMap.put("data", dataList);
		retMap.put("name1", "元");
		
		return JSON.toJSONString(retMap);
	}
	
	/**
	 * 获取社保福利成本
	 * 
	 * @param jobNumber
	 * @return
	 */
	@RequestMapping(value = "/socialSecurityWelfareCost")
	@ResponseBody
	public String getSocialSecurityWelfareCost(
			@RequestParam(value = "jobNumber") String jobNumber,
			@RequestParam(value = "startTime") String startDate, 
    		@RequestParam(value = "endTime") String endDate ) {
		
		if( null == startDate || "".equals(startDate) || null == endDate || "".equals(endDate)) {
            String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
            startDate = year + "-01";
            endDate = year + "-12";
        }
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		dataMap.put("name", "社保福利");
		dataMap.put("type", "line");
		dataMap.put("data", employeeDetailsService.getSocialSecurityWelfareCost(jobNumber, startDate, endDate));
		dataList.add(dataMap);
		retMap.put("title", new String[] {"社保福利"});
		retMap.put("dates", getMonthList(startDate, endDate));
		retMap.put("data", dataList);
		retMap.put("name1", "元");
		
		return JSON.toJSONString(retMap);
	}
	
	private List<String> getMonthList(String startMonth, String endMonth) {
		List<String> monthList = new ArrayList<String>();
		
		Calendar startMonthCal = CalendarUtil.convertToCal(startMonth);
		Calendar endMonthCal = CalendarUtil.convertToCal(endMonth);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
		while(!endMonthCal.equals(startMonthCal)) {
			monthList.add(sdf.format(startMonthCal.getTime()));
			startMonthCal.add(Calendar.MONTH, 1);
		}
		monthList.add(sdf.format(startMonthCal.getTime()));
		
		return monthList;
	}

}
